package de.hsos.bose.skillshare.view;

import java.awt.Color;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.JTableHeader;

import de.hsos.bose.skillshare.model.core.DataAccess;
import de.hsos.bose.skillshare.model.core.combobox.AssignmentComboBoxModel;
import de.hsos.bose.skillshare.model.core.combobox.RatingComboBoxModel;
import de.hsos.bose.skillshare.model.core.table.AssignmentTableModel;
import de.hsos.bose.skillshare.model.core.table.EmployeeTableModel;
import de.hsos.bose.skillshare.model.objects.Mitarbeiter;

public class UserRightsPanel extends BasePanel {

	private static final long serialVersionUID = 1L;
	private static final String resSetupFrame = "User Management";
	private static final String resEmployeeFrame = "User Assignment";
	private static final String resHint = "Enter username, password and roll";
	private static final String resHintUsername = "Username exists";

	protected JTable mainTable;
	protected JPanel setupPanel;
	protected JPanel employeePanel;
	protected JTextField txtLogin;
	protected JTextField txtPassword;
	protected JComboBox cbmRoll;
	protected JLabel lblLogin;
	protected JLabel lblPassword;
	protected JLabel lblRoll;
	protected JButton btnSave;
	protected JButton btnDelete;
	protected JScrollPane scroller;
	protected JTable assignmentTable;
	protected JComboBox cbmEmployees;
	protected JButton btnAssign;
	protected JButton btnAssignDelete;
	protected JScrollPane assignmentScroller;
	protected JLabel lblHint;
	protected Border borderHint;
	protected Border borderTextField;

	protected EmployeeTableModel model;
	protected RatingComboBoxModel ratingComboModel;
	protected Mitarbeiter currentMita;
	protected AssignmentTableModel assignmentModel;
	protected AssignmentComboBoxModel assignmentComboModel;
	protected Mitarbeiter currentAssignMita;

	public UserRightsPanel() {
		borderTextField = BorderFactory.createLineBorder(Color.black);
		borderHint = BorderFactory.createLineBorder(Color.red, 2);
		initMainTable();
		initSetupTable();
		initEmployeeTable();
	}

	private void initSetupTable() {

		Container pane = this;
		Insets insets = pane.getInsets();

		setupPanel = new JPanel();
		setupPanel.setBorder(BorderFactory.createTitledBorder(resSetupFrame));
		// setupTable.setPreferredSize(new Dimension(250, 250));
		// setupTable.setLocation(50, 225);
		setupPanel.setLayout(null);
		lblLogin = new JLabel("Username", JLabel.RIGHT);
		lblLogin.setBounds(10 + insets.left, 20 + insets.top, 70, 25);
		txtLogin = new JTextField("", 1);
		txtLogin.setBorder(BorderFactory.createLineBorder(Color.black));
		txtLogin.setBounds(90 + insets.left, 20 + insets.top, 150, 25);
		lblPassword = new JLabel("Password", JLabel.RIGHT);
		lblPassword.setBounds(10 + insets.left, 50 + insets.top, 70, 25);
		txtPassword = new JTextField("", 1);
		txtPassword.setBorder(BorderFactory.createLineBorder(Color.black));
		txtPassword.setBounds(90 + insets.left, 50 + insets.top, 150, 25);
		lblRoll = new JLabel("Role", JLabel.RIGHT);
		lblRoll.setBounds(10 + insets.left, 80 + insets.top, 70, 25);
		ratingComboModel = new RatingComboBoxModel();
		cbmRoll = new JComboBox(ratingComboModel);
		cbmRoll.setBorder(BorderFactory.createLineBorder(Color.black));
		cbmRoll.setBounds(90 + insets.left, 80 + insets.top, 150, 25);
		btnSave = new JButton("Save");
		btnSave.setBounds(10 + insets.left, 110 + insets.top, 100, 25);
		btnSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (txtLogin.getText() != null && txtPassword.getText() != null
						&& cbmRoll.getSelectedIndex() > -1) {
					if (!txtLogin.getText().isEmpty()
							&& !txtPassword.getText().isEmpty()) {
						if (!currentMita.isNew()
								&& txtLogin.getText().equals(
										currentMita.getLogin())) {
							(new DataAccess()).deleteUser(currentMita);
							currentMita.setLogin(txtLogin.getText());
							currentMita.setPasswort(txtPassword.getText());
							currentMita.setRollID(ratingComboModel
									.getCurrentItem().getId());
							(new DataAccess()).insertUser(currentMita);
							model.loadUsers();
							mainTable.repaint();
							if (currentMita.getRollID() == 2) {
								updateAsignmentsTable();
							}
							txtLogin.setBorder(borderTextField);
							txtPassword.setBorder(borderTextField);
							cbmRoll.setBorder(borderTextField);
							lblHint.setVisible(false);
						} else {
							if ((new DataAccess()).validateUsername(txtLogin
									.getText()) < 0) {
								(new DataAccess()).deleteUser(currentMita);
								currentMita.setLogin(txtLogin.getText());
								currentMita.setPasswort(txtPassword.getText());
								currentMita.setRollID(ratingComboModel
										.getCurrentItem().getId());
								(new DataAccess()).insertUser(currentMita);
								model.loadUsers();
								mainTable.repaint();
								if (currentMita.getRollID() == 2) {
									updateAsignmentsTable();
								}
								txtLogin.setBorder(borderTextField);
								txtPassword.setBorder(borderTextField);
								cbmRoll.setBorder(borderTextField);
								lblHint.setVisible(false);
							} else {
								txtLogin.setBorder(borderHint);
								txtPassword.setBorder(borderHint);
								cbmRoll.setBorder(borderHint);
								lblHint.setText(resHintUsername);
								lblHint.setVisible(true);
							}
						}

					} else {
						txtLogin.setBorder(borderHint);
						txtPassword.setBorder(borderHint);
						cbmRoll.setBorder(borderHint);
						lblHint.setText(resHint);
						lblHint.setVisible(true);
					}
				} else {
					txtLogin.setBorder(borderHint);
					txtPassword.setBorder(borderHint);
					cbmRoll.setBorder(borderHint);
					lblHint.setText(resHint);
					lblHint.setVisible(true);
				}
			}
		});
		btnDelete = new JButton("Delete");
		btnDelete.setBounds(120 + insets.left, 110 + insets.top, 100, 25);
		btnDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				(new DataAccess()).deleteUser(currentMita);
				model.loadUsers();
				mainTable.repaint();
				txtLogin.setText(null);
				txtPassword.setText(null);
				ratingComboModel.setSelectedItem(-1);
				currentMita.setLogin(null);
				currentMita.setPasswort(null);
				currentMita.setRollID(0);
				updateAsignmentsTable();
				txtLogin.setBorder(borderTextField);
				txtPassword.setBorder(borderTextField);
				cbmRoll.setBorder(borderTextField);
				lblHint.setVisible(false);
			}
		});
		lblHint = new JLabel();
		lblHint.setText(resHint);
		lblHint.setForeground(Color.RED);
		lblHint.setVisible(false);
		lblHint.setBounds(10 + insets.left, 140 + insets.top, 220, 25);

		setupPanel.add(lblLogin);
		setupPanel.add(txtLogin);
		setupPanel.add(lblPassword);
		setupPanel.add(txtPassword);
		setupPanel.add(lblRoll);
		setupPanel.add(cbmRoll);
		setupPanel.add(btnSave);
		setupPanel.add(btnDelete);
		setupPanel.add(lblHint);
		setupPanel.setBounds(10 + insets.left, 240 + insets.top, 250, 200);
		add(setupPanel);

	}

	private void initMainTable() {

		Container pane = this;
		Insets insets = pane.getInsets();

		model = new EmployeeTableModel();

		mainTable = new JTable(model);
		mainTable.setDragEnabled(false);
		mainTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		mainTable.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (e.getValueIsAdjusting()) {
							if (e.getSource() == mainTable.getSelectionModel()
									&& mainTable.getRowSelectionAllowed()) {
								currentMita = model
										.getMitarbeiter(((DefaultListSelectionModel) e
												.getSource())
												.getLeadSelectionIndex());
								if (currentMita.getLogin() == null
										|| currentMita.getLogin().isEmpty()) {
									currentMita.setNew(true);
								} else {
									currentMita.setNew(false);
								}
								txtLogin.setText(currentMita.getLogin());
								txtPassword.setText(currentMita.getPasswort());
								int rollId = currentMita.getRollID();
								cbmRoll.getModel().setSelectedItem(rollId);
								if (rollId > 1) {
									employeePanel.setVisible(true);
									assignmentModel.loadEmpoyees(currentMita
											.getId());
									assignmentComboModel.loadEmpoyees(
											currentMita.getId(),
											currentMita.getRollID());

									cbmEmployees.revalidate();
									// cbmEmployees.repaint();
									assignmentTable.revalidate();
									assignmentTable.repaint();
								} else {
									employeePanel.setVisible(false);
									assignmentComboModel.loadEmpoyees(-1,
											currentMita.getRollID());
									currentAssignMita = null;
								}
								currentAssignMita = null;
							}
						} else {
							txtLogin.setBorder(borderTextField);
							txtPassword.setBorder(borderTextField);
							cbmRoll.setBorder(borderTextField);
							lblHint.setVisible(false);
						}
					}
				});
		mainTable.setRowHeight(20);
		mainTable.getColumnModel().getColumn(EmployeeTableModel.FIRSNAME_INDEX)
				.setPreferredWidth(150);
		mainTable.getColumnModel().getColumn(EmployeeTableModel.NAME_INDEX)
				.setPreferredWidth(150);
		mainTable.getColumnModel()
				.getColumn(EmployeeTableModel.OCCUPATION_INDEX)
				.setPreferredWidth(150);
		mainTable.getColumnModel().getColumn(EmployeeTableModel.USERNAME_INDEX)
				.setPreferredWidth(100);
		mainTable.getColumnModel().getColumn(EmployeeTableModel.PASSWORD_INDEX)
				.setPreferredWidth(100);
		mainTable.getColumnModel().getColumn(EmployeeTableModel.ROLL_INDEX)
				.setPreferredWidth(100);

		mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		mainTable.setPreferredScrollableViewportSize(mainTable
				.getPreferredSize());

		JTableHeader header = mainTable.getTableHeader();
		header.setResizingAllowed(false);
		header.setReorderingAllowed(false);

		scroller = new JScrollPane(mainTable);
		scroller.setBounds(10 + insets.left, 20 + insets.top, 768, 200);

		this.setLayout(null);
		this.add(scroller);

	}

	private void initEmployeeTable() {

		Container pane = this;
		Insets insets = pane.getInsets();

		assignmentModel = new AssignmentTableModel();
		assignmentComboModel = new AssignmentComboBoxModel();
		cbmEmployees = new JComboBox(assignmentComboModel);
		cbmEmployees.setBounds(10 + insets.left, 20 + insets.top, 348, 25);
		btnAssign = new JButton("Assign");
		btnAssign.setBounds(378 + insets.left, 20 + insets.top, 100, 25);
		btnAssign.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Mitarbeiter mita = ((AssignmentComboBoxModel) cbmEmployees
						.getModel()).getItem();
				if (mita != null) {
					(new DataAccess()).insertAssignment(currentMita.getId(),
							mita.getId());
					assignmentModel.loadEmpoyees(currentMita.getId());
					assignmentComboModel.loadEmpoyees(currentMita.getId(),
							currentMita.getRollID());
					// cbmEmployees.revalidate();
					cbmEmployees.repaint();
					// assignmentTable.revalidate();
					assignmentTable.repaint();
				}
			}
		});
		btnAssignDelete = new JButton("Delete");
		btnAssignDelete.setBounds(378 + insets.left, 165 + insets.top, 100, 25);
		btnAssignDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				(new DataAccess()).deleteAssignment(currentMita.getId(),
						currentAssignMita.getId());
				assignmentModel.loadEmpoyees(currentMita.getId());
				assignmentComboModel.loadEmpoyees(currentMita.getId(),
						currentMita.getRollID());
			}
		});
		assignmentTable = new JTable(assignmentModel);
		assignmentTable.setDragEnabled(false);
		assignmentTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		assignmentTable.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (e.getValueIsAdjusting()) {
							if (e.getSource() == assignmentTable
									.getSelectionModel()
									&& assignmentTable.getRowSelectionAllowed()) {
								currentAssignMita = assignmentModel
										.getMitarbeiter(((DefaultListSelectionModel) e
												.getSource())
												.getLeadSelectionIndex());

								// cbmEmployees.revalidate();
								// assignmentTable.revalidate();
								// assignmentTable.revalidate();
							}
						}
					}
				});
		assignmentTable.setRowHeight(20);
		assignmentTable.getColumnModel()
				.getColumn(AssignmentTableModel.FIRSNAME_INDEX)
				.setPreferredWidth(150);
		assignmentTable.getColumnModel()
				.getColumn(AssignmentTableModel.NAME_INDEX)
				.setPreferredWidth(150);
		assignmentTable.getColumnModel()
				.getColumn(AssignmentTableModel.OCCUPATION_INDEX)
				.setPreferredWidth(150);
		assignmentTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		assignmentTable.setPreferredScrollableViewportSize(assignmentTable
				.getPreferredSize());

		JTableHeader header = assignmentTable.getTableHeader();
		header.setResizingAllowed(false);
		header.setReorderingAllowed(false);

		assignmentScroller = new JScrollPane(assignmentTable);
		assignmentScroller.setBounds(10 + insets.left, 55 + insets.top, 468,
				100);

		employeePanel = new JPanel();
		employeePanel.setLayout(null);
		employeePanel.setVisible(false);
		employeePanel.setBorder(BorderFactory
				.createTitledBorder(resEmployeeFrame));
		employeePanel.setBounds(280 + insets.left, 240 + insets.top, 498, 200);
		employeePanel.add(cbmEmployees);
		employeePanel.add(btnAssign);
		employeePanel.add(btnAssignDelete);
		employeePanel.add(assignmentScroller);

		this.add(employeePanel);
	}

	protected void updateAsignmentsTable() {

	}

}
